package com.own.demo.坐标转换;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.enumv2.GeoPoint;
import com.own.demo.enumv2.GpsTypeEnums;
import com.own.demo.utils.CommWriterFileInfo;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.vo.Bounds;
import com.own.demo.writerfile.GeoJsonToOsmXML;

import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;

public class 长沙主城区坐标转换 {
    private static String geojsonUrl="E:\\ownCode\\UMA\\数据\\数据static";
    private static String geoJsonName="\\districtData-84-02.geojson";

    private static String osmUrl="E:\\ownCode\\UMA\\数据\\数据static";
    private static String osmName="\\districtData-84-02.osm";

    public static void main(String[] args) {

        File geoZJ = new File("E:\\ownCode\\UMA\\数据\\数据static\\districtData.json");
        JSONObject  geoData = JSONObject.parseObject(FileToStringUtils.txt2String(geoZJ));
        JSONArray jsonArray= geoData.getJSONArray("features");
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            JSONObject geometry= jsonObject.getJSONObject("geometry");
            JSONArray coordinates=geometry.getJSONArray("coordinates");
            int row=coordinates.size();

            for(int h=0;h<row;h++){
                JSONArray temp1= coordinates.getJSONArray(h);
                JSONArray pointsAll=new JSONArray(temp1.size());
                for(int j=0;j<temp1.size();j++) {
                    JSONArray points=temp1.getJSONArray(j);
                    double lon = points.getDouble(0);
                    double lat = points.getDouble(1);
                    GeoPoint gpsPoint = GpsTypeEnums.gpsToGcJo2New(lat, lon, GpsTypeEnums.COORDINATE_SYSTEM_BD0911.getId());

                    points.clear();

                    JSONArray temp02 = new JSONArray(4);
                    temp02.add(gpsPoint.getX());
                    temp02.add(gpsPoint.getY());
                    pointsAll.add(temp02);
                }
                coordinates.getJSONArray(h).clear();
                coordinates.getJSONArray(h).addAll(pointsAll);
            }
        }
        String json=JSONObject.toJSONString(geoData);
        writerFoFile(json,geojsonUrl,geoJsonName);
    }

    public static void writerFoFile(String data, String folderPath, String fileName) {
        try {
            File folder = new File(folderPath);

            if (!folder.exists()) {
                folder.mkdirs();
            }
            BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(folderPath + "\\" + fileName, false);
            CommWriterFileInfo.writer(bufferedWriter, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
